//增加购物车列表中的行

function addr(button){

//从商品列表中获取元素

var tr = button.parentNode.parentNode;

var td = tr.getElementsByTagName("td");

var goods = td[0].innerHTML;

var des = td[1].innerHTML;

var price = td[2].innerHTML;

var sele = td[3].innerHTML;

//使用insertRow（）函数增加行

var tbody=document.getElementById("goods");

  var row=tbody.insertRow();

//将获取的对应的元素放入新增加的行中

row.innerHTML = "<td>"+goods+"</td>"+

"<td>"+des+"</td>"+

"<td>"+price+"</td>"+

"<td>"+sele+"</td>"+

"<td align='center'>"+

"<input type='button' value=' - ' id='jian' onclick=jian(this)>"+

  "<input id='text' type='text' size='1' value='1' />"+

  "<input type='button' value=' + ' id='add' onclick=add(this)>"+"</td>"+

  "<td>"+price+"</td>"+

  "<td align='center'>"+

    "<input type='button' value='删除' class='add1' onclick=dele(this)>"+

  "</td>"

  //计算总价函数

  total();

}

//删除函数

function dele(dele){

dele.parentNode.parentNode.remove();

total();

}

//数量减少函数

function jian(jian){

//获取jian父节点的所有input标签

var inputs = jian.parentNode.getElementsByTagName("input");

//取出放在第二列的数量

amount = inputs[1].value;

//转换为整型

var amount1 = parseInt(amount);

//最小数量为1

if(amount1 == 1){

return;

}

inputs[1].value=amount1-1;

//根据数量的变化，计算出当前数量对应的金额

var tr = jian.parentNode.parentNode;

var tds = tr.getElementsByTagName("td");

var price = parseFloat(tds[2].innerHTML);

var sum = price*amount1;

tds[5].innerHTML=sum-price;

total();

}

function add(add){

var inputs = add.parentNode.getElementsByTagName("input");

amount = inputs[1].value;

var amount1 = parseInt(amount);

inputs[1].value=amount1+1;

var tr = add.parentNode.parentNode;

var tds = tr.getElementsByTagName("td");

var price = parseFloat(tds[2].innerHTML);

var sum = price*amount1;

tds[5].innerHTML=sum+price;

total();

}

//计算总价函数

function total() {

var tbody = document.getElementById("goods");

//取出tbody中所有tr标签

var trs = tbody.getElementsByTagName("tr");

var sum = 0;

//遍历trs数组，并计算每一个tr标签中金额的总和

for(var i = 0; i < trs.length; i++) {

var tds = trs[i].getElementsByTagName("td");

var m = tds[5].innerHTML;

sum += parseFloat(m);

}

var total = document.getElementById("total");

total.innerHTML = sum;

}

function prepareGallery() {

if (!document.getElementsByTagName) return false;

if (!document.getElementById) return false;

if (!document.getElementById("shopping")) return false;

var gallery = document.getElementById("shopping");

var links = gallery.getElementsByTagName("button");

for ( var i=0; i < links.length; i++) {

  links[i].onclick = function() {

  return addr(this);

}

}

}

function addLoadEvent(func) {

var oldonload = window.onload;

if (typeof window.onload != 'function') {

  window.onload = func;

} else {

  window.onload = function() {

  oldonload();

  func();

  }

}

}

addLoadEvent(prepareGallery);